﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class BalanceCenter_Net2 : PageBase
{
    public string[] MemberBack = new string[7];
    public string[] NextID = new string[7];
    public string ParentStr = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        long id = Shove._Convert.StrToLong(this.Request["id"], -1);
        if (id < 0)
        {
            id = _Customer.ID;
        }

        if (id < _Customer.ID)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        BindData(id);
    }

    #region Web 窗体设计器生成的代码

    override protected void OnInit(EventArgs e)
    {
        isRequestLogin = true;                                  //是否需要登录
        isAtFramePageLogin = true;                              //是框架页
        RequestLoginPage = this.Request.Url.AbsoluteUri;        //获取决对Url        
        //RequestCompetences = Competences.BuildCompetencesList(Competences.Net);

        base.OnLoad(e);
    }

    #endregion

    private void BindData(long id)
    {
        DataSet ds = new DataSet();
        DAL.Procedures.P_GetNet(ref ds, id);

        //查询用户名
        string Return = "";
        int Result = 0;

        Customer cu1 = new Customer(_Site.ID);
        cu1.ID = id;
        Result = cu1.GetCustomerInformationByID(ref Return);

        if (Result != 0)
        {
            Shove._Web.JavaScript.Alert(this.Page, "搜索的会员ID不存在。");

            return;
        }

        lbCustomerName.Text = cu1.CustomerName;
        lbCustomerNo.Text = cu1.CustomerNO;
        lbPV.Text = cu1.PV.ToString();

        if (ds == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        if (ds.Tables.Count == 0)
        {
            PublicFunction.GoError(ErrorNumber.NoData, "暂无数据", this.GetType().BaseType.FullName);

            return;
        }

        DataTable dt = ds.Tables[0];

        if (dt.Columns.Count == 0)
        {
            PublicFunction.GoError(ErrorNumber.NoData, "暂无数据", this.GetType().BaseType.FullName);

            return;
        }

        for (int i = 1; i < 3; i++)
        {
            DataRow[] dr = dt.Select("Locate=" + (i + 1).ToString());
            if (dr.Length < 1)
            {
                MemberBack[i] = "#FFFFFF";
            }
            else
            {
                ((Panel)this.FindControl("Panel" + (i + 1).ToString())).Visible = true;

                long UserID = Shove._Convert.StrToLong(dr[0]["id"].ToString(), -1);
                if (UserID == -1)
                {
                    MemberBack[i] = "#FFFFFF";
                    ((Label)this.FindControl("labName" + (i + 1).ToString())).Text = "<a class='li8' href='Buy.aspx?PID=" + dr[0]["ParentID"].ToString() + "&LR=" + dr[0]["Location"].ToString() + "'></a>";
                }
                else
                {                    
                    bool isActivation = Shove._Convert.StrToBool(dr[0]["isActivation"].ToString(), false);

                    if (isActivation)
                    {
                        MemberBack[i] = "#fad6d8";
                    }
                    else
                    {
                        MemberBack[i] = "#f8f7c9";
                    }

                    //((Label)this.FindControl("labBDZX" + (i + 1).ToString())).Text = (Shove._Convert.StrToBool(dr[0]["isSurrogate"].ToString(), false) ? "<img src='../Images/Surrogate.gif' border='0'>" : "");
                    ((Label)this.FindControl("labName" + (i + 1).ToString())).Text = "<a class='li8' href='Net2.aspx?id=" + dr[0]["id"].ToString() + "'>" + dr[0]["CustomerNo"].ToString() + "</a>";

                    //查询用户名     
                    //Customer cu2 = new Customer(_Site.ID);
                    //cu2.ID = Shove._Convert.StrToLong(dr[0]["id"].ToString(), -1);
                    //Result = cu2.GetCustomerInformationByID(ref Return);

                    //if (Result != 0)
                    //{
                    //    Shove._Web.JavaScript.Alert(this.Page, "搜索的会员ID不存在。");

                    //    return;
                    //}

                    long L = 0;
                    long R = 0;
                    long L_CurrentMonth = 0;
                    long R_CurrentMonth = 0;    //月拓展店数
                    long L_CurrentWeek = 0;     
                    long R_CurrentWeek = 0;     //周拓展店数
                    double L_PV = 0;
                    double R_PV = 0;            //本月销售PV
                  
                    if (DAL.Procedures.P_GetScoreForCustomer2(id, ref L, ref R, ref L_CurrentMonth, ref R_CurrentMonth, ref L_CurrentWeek, ref R_CurrentWeek, ref L_PV, ref R_PV) < 0)
                    {
                        ((Label)this.FindControl("labSL" + (i + 1).ToString())).Text = "0";
                        ((Label)this.FindControl("labDL" + (i + 1).ToString())).Text = "0";
                        ((Label)this.FindControl("labWL" + (i + 1).ToString())).Text = "0";
                    }
                    else
                    {
                        labSL2.Text = L_CurrentMonth.ToString();
                        labSL3.Text = R_CurrentMonth.ToString();

                        labDL2.Text = L_CurrentWeek.ToString();
                        labDL3.Text = R_CurrentWeek.ToString();

                        labWL2.Text = L_PV.ToString();
                        labWL3.Text = R_PV.ToString();
                    }
                }
            }
        }
    }

    protected void btnSerch_Click(object sender, EventArgs e)
    {
        if (this.tbSerchUserName.Text.Trim().Equals(""))
        {
            Shove._Web.JavaScript.Alert(this, "请输入要搜索的关键字");

            return;
        }

        string Return = "";
        int Result = 0;

        Customer cu = new Customer(_Site.ID);
        cu.CustomerNO = tbSerchUserName.Text.Trim();
        Result = cu.GetCustomerInformationByNO(ref Return);

        if (Result != 0)
        {
            Shove._Web.JavaScript.Alert(this.Page, "搜索的会员ID不存在。");

            return;
        }

        // 检测是否是会员的下线        
        DataTable dt = DAL.Functions.F_GetUserNet(_Customer.ID);

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (dt.Rows[i]["ID"].ToString() == cu.ID.ToString())
            {
                Server.Transfer("Net2.aspx?id=" + cu.ID);

                break;
            }
        }

        Shove._Web.JavaScript.Alert(this.Page, "会员不是您的下线");

        return;
    }
}